package org.codebase.java.designpattern.proxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * jdk动态代理中间类
 **************************************************
 * ClassName:RpcInvocationHandler

 * @author:xiaozhichao
 * @date:2018年8月1日
 **************************************************
 */
public class RpcInvocationHandler implements InvocationHandler {

	private String protocol;
	private String host;
	private int port;
	
	public RpcInvocationHandler(String protocol, String host, int port) {
		super();
		this.protocol = protocol;
		this.host = host;
		this.port = port;
	}

	@Override
	public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
		Class<?>[] parameterTypes = method.getParameterTypes();
		String methodName = method.getName();
		String filterName = "toString";
		if(filterName.equals(methodName) && parameterTypes.length == 0){
			return method.invoke(proxy, args);
		}
		URL url = new URL(protocol,host,port,null);
		@SuppressWarnings("unused")
		HttpURLConnection connection = (HttpURLConnection) url.openConnection();
		return null;

	}

	public String getProtocol() {
	
		return protocol;
	}

	public void setProtocol(String protocol) {
	
		this.protocol = protocol;
	}

	public String getHost() {
	
		return host;
	}

	public void setHost(String host) {
	
		this.host = host;
	}

	public int getPort() {
	
		return port;
	}

	public void setPort(int port) {
	
		this.port = port;
	}

	
}
